﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using WaiGaoQiaoWeb.Models;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class AccountController : Controller
    {

        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        // **************************************
        // URL: /Account/LogOn
        // **************************************

        public ActionResult LogOn(LogOnModel m)
        {
            return View(m);
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                LoggingInfo loginfo = new LoggingInfo();
                string password = loginfo.getlogpassword(model.LogName);
                if (password.Trim() == model.LogPassword.Trim())
                {
                    System.Web.HttpContext.Current.Session["user"] = model;
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    //ModelState.AddModelError("", "The user name or password provided is incorrect.");
                    model.logError = "用户名称或密码有误";
                    return View("LogOn", model);
                }
            }
            return View("LogOn", model);
        }

        // **************************************
        // URL: /Account/LogOff
        // **************************************

        //public ActionResult LogOff()
        //{
        //    FormsService.SignOut();

        //    return RedirectToAction("Index", "Home");
        //}

        // **************************************
        // URL: /Account/Register
        // **************************************

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                LoggingInfo Loginfo = new LoggingInfo()
                {
                    LogName = model.LogName,
                    LogPassword = model.LogPassword,
                    PasswordConfirm = model.PasswordConfirm,
                    PasswordHint = model.PasswordHint
                };
                result = Loginfo.SaveLog(Loginfo);
            }
            return Json(result);
        }

        public ActionResult DeleteLogUser(string logid)
        {
            LoggingInfo loginfo = new LoggingInfo();
            int rowcount = loginfo.DeleteLog(logid);
            return Json(rowcount);
        }
        /// <summary>
        /// 登录用户列表页面
        /// </summary>
        /// <returns></returns>
        public ActionResult LoginfoIndex()
        {
            return View();
        }

        public ActionResult GetLogList(PageVM pvm)
        {
            LoggingInfo loginfo = new LoggingInfo(true);
            IList<LoggingInfo> ulist = loginfo.GetLogList(pvm.rp, pvm.page);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = loginfo._LogUserCount;
            List<string> v;
            foreach (LoggingInfo linfo in ulist)
            {
                #region
                v = new List<string>();
                v.Add(linfo.LogName);
                v.Add(linfo.LogPassword);
                v.Add(linfo.PasswordHint);
                v.Add(linfo.LogDt.ToString());
                v.Add(linfo.IsLocked);
                v.Add(linfo.LockDt.ToString());
                v.Add(linfo.RoleName.ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = linfo.LogName,
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        // **************************************
        // URL: /Account/LogUserBindRole
        // 登录用户绑定到角色
        // **************************************
        public ActionResult UserBindRole()
        {
            return View();
        }

        public ActionResult GetAllRoles()
        {
            RolesInfo role = new RolesInfo(true);
            IList<RolesInfo> rlist = role.GetAllRoles();
            List<JsonTreeNode> nodes = new List<JsonTreeNode>();
            foreach (var item in rlist)
            {
                JsonTreeNode cnode = new JsonTreeNode();
                cnode.id = item.RoleId;
                cnode.text = item.RoleName;
                cnode.value = item.RoleMeno == null ? "" : item.RoleMeno;
                cnode.hasChildren = false;
                cnode.classes = "system";
                cnode.isexpand = false;
                cnode.complete = true;
                nodes.Add(cnode);
            }
            return Json(nodes);
        }
        /// <summary>
        /// 绑定用户到角色
        /// </summary>
        /// <param name="logid"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult BindUserToRole(string logid, string roleid)
        {
            LoggingInfo loginfo = new LoggingInfo();
            int rowcount = loginfo.BindUserToRole(logid, roleid);
            return Json(rowcount);
        }
        /// <summary>
        /// 删除绑定
        /// </summary>
        /// <param name="logid"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult NotBindUserToRole(string logid, string roleid)
        {
            LoggingInfo loginfo = new LoggingInfo();
            int rowcount = loginfo.NotBindUserToRole(logid, roleid);
            return Json(rowcount);
        }
        // **************************************
        // URL: /Account/ChangePassword
        // **************************************

        //[Authorize]
        //public ActionResult ChangePassword()
        //{
        //    ViewBag.PasswordLength = MembershipService.MinPasswordLength;
        //    return View();
        //}

        //[Authorize]
        //[HttpPost]
        //public ActionResult ChangePassword(ChangePasswordModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
        //        {
        //            return RedirectToAction("ChangePasswordSuccess");
        //        }
        //        else
        //        {
        //            ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
        //        }
        //    }

        //    // If we got this far, something failed, redisplay form
        //    ViewBag.PasswordLength = MembershipService.MinPasswordLength;
        //    return View(model);
        //}

        // **************************************
        // URL: /Account/ChangePasswordSuccess
        // **************************************

        //public ActionResult ChangePasswordSuccess()
        //{
        //    return View();
        //}

    }
}
